#	資産の収益率の平均と分散、資産間の共分散、資産への投資比率を入力としそのポートフォリオの平均、分散を出力する。

#	@param	earning_rate_means			資産ßの収益率の平均ベクトル
#	@param	earning_rate_variances		資産の収益率の分散ベクトル
#	@param	earning_covariaces			資産の収益率の共分散行列
#	@param	invest_ratio				資産への投資比率
#	@return ポートフォリオの平均と分散を格納したリスト $mean, $variance, $error_message
calc_portfolio_performance <- function( earning_rate_means, earning_rate_variances, earning_rate_covariaces, invest_ratio )
{
	data_count <- length( earning_rate_means )
	portfolio_mean <- NA
	portfolio_variance <- NA

	# 前提チェック
	if ( data_count != length( earning_rate_variances ) ||
		 data_count != ncol( earning_rate_covariaces )  ||
		 data_count != nrow( earning_rate_covariaces )  ||
		 data_count != length( invest_ratio ) )
	{
		return( list( mean=NA, variance=NA, error_message="invalide data count" ) )
	}

	portfolio_mean <- 0
	portfolio_variance <- 0

	counter = 1
	for ( i in 1:data_count )
	{
		portfolio_mean = portfolio_mean + invest_ratio[ i ] * earning_rate_means[ i ]

		for ( j in 1:data_count )
		{
			portfolio_variance = portfolio_variance + invest_ratio[ i ] * invest_ratio[ j ] * earning_rate_covariaces[i, j]
#			cat( "i:", i, "j:", j,
#				"invest_ratio[i]:", invest_ratio[i], "invest_ratio[j]:", invest_ratio[j], 
#				"earning_rate_covariaces[i,j]:", earning_rate_covariaces[i,j], "\n" )
		}
	}
	return( list( mean=portfolio_mean, variance=portfolio_variance, error_message=NULL ) )
}
